package com.yunxi.product.controller;

import com.yunxi.product.bean.Product;
import com.yunxi.product.service.ProductService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品相关 API
 *
 * @author: lidy
 * @date: 2025-01-17 10:25
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping("/{id}")
    public Product getProduct(@PathVariable("id") Long productId,
                              HttpServletRequest request) {
        String xtoken = request.getHeader("X-Token");
        System.out.println("请求进来了......xtoken=" + xtoken);
        // 测试熔断 - 慢调用比例
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        // 测试熔断 - 异常比例
//        int i = 1 / 0;

        return productService.getProduct(productId);
    }

}
